#pragma pack_matrix(row_major)

#include "Defs/types.hlsli"
#include "Defs/ConstantBuffers.hlsli"
#include "Defs/Textureing.hlsli"
#include "Defs/LightSupport.hlsli"

float4 main(float4 position : SV_POSITION, float2 uv : TEXCOORD) : SV_TARGET
{
	float2 scrPos = (uv - .5f) * 2.f;
	float3 diffuse = diffuseMap.Sample(linearClampSampler, uv).rgb;
	float depth = depthMap.Sample(linearClampSampler, uv);
	float alphaOutput = normalMap.Sample(linearClampSampler, uv).a;
	float3 normal = normalMap.Sample(linearClampSampler, uv).rgb;
		normal = (normal - .5f) * 2.f;


	float3 posWorld = CalculateWorldSpacePosition(scrPos, depth, gInvViewProj);
		float3 L = posWorld - gPointLight.position;
		float range = length(L);
	float rangeMul = 1.f - saturate(range / gPointLight.range);

	L = normalize(L);
	float nDotL = dot(normal, L);




	return float4(diffuse * rangeMul, alphaOutput);
#if 0
	float3 specular = specularMap.Sample(linearClampSampler, uv);
	float3 diffuse = diffuseMap.Sample(linearClampSampler, uv);
	float ambient = normalMap.Sample(linearClampSampler, uv).a;
	float3 normal = normalMap.Sample(linearClampSampler, uv);
		float outputAlpha = normalMap.Sample(linearClampSampler, uv).a;
	normal = (normal - .5f) * 2.f;
	float depth = depthMap.Sample(linearClampSampler, uv).a;
	float4 posWorld = CalculateWorldSpacePosition(position.xy, depth, gInvViewProj);

		float3 direction = gPointLight.position - posWorld;
		float nDotL = saturate(dot(direction, normal));
	float3 directionToCamera = normalize(gCameraPosition - posWorld);
		float3 reflectionVector = reflect(normal, directionToCamera);
		float specular_nDotL = saturate(dot(reflectionVector, directionToCamera)) * nDotL;

	float3 finalDiffuse = diffuse * gPointLight.diffuseColor * nDotL;
		float3 finalSpecular = diffuse * gPointLight.specularColor * specular_nDotL;
		return float4(finalDiffuse + finalSpecular, outputAlpha);
#endif
}